package com.glory.hzlionpda.base;

import com.glory.hzlionpda.activity.StartLotActivity;
import com.glory.hzlionpda.bean.MessageBean;
import com.glory.hzlionpda.bean.MessageObject;
import com.glory.hzlionpda.utils.NetUtils;
import com.glory.hzlionpda.utils.VibratorUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseHomeActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author yi.zhang
 * @date 2019/7/18
 * @desc
 */
public abstract class BasePollingActivity extends FreeUI_BaseHomeActivity {

    private static List<String> allAlertMessageIdList = new ArrayList<>();
    private Timer timer;
    private boolean isVirating = false;

    @Override
    protected void onStart() {
        super.onStart();
        if (Constant.AREA.length() > 0) {
            startTimer();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void startTimer() {
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    requestMessageData();
                }
            }, 0, 2000);
        }
    }

    private void requestMessageData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("AREAGROUP", Constant.AREA);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestGetNet("AREALOGMESSAGEREQUEST", this, jsonObject, new FreeUI_EntityCallBack<MessageBean>(
                new TypeToken<MessageBean>() {
                }.getType()) {
            @Override
            public void onSuccess(Response<MessageBean> response) {
                if (response.body().isSuccess(getApplicationContext())) {
                    dealMessage(response.body().getLOGMESSAGETEXTLIST());
                }
            }

            @Override
            public void onError(Response<MessageBean> response) {
                super.onError(response);
                loadError(response.getException(), "AREALOGMESSAGEREQUEST");
            }
        });
    }

    private void dealMessage(List<MessageObject> data) {
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getISALERT().equals("1")) {
                boolean isShow = false;
                for (String str : allAlertMessageIdList) {
                    if (str.equals(data.get(i).getMESSAGEID())) {
                        isShow = true;
                        break;
                    }
                }
                if (!isShow) {
                    allAlertMessageIdList.add(data.get(i).getMESSAGEID());
                    FreeUI_GeneralFragmentDialog warnDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "我知道了", data.get(i).getLOGMESSAGETEXT(), new FreeUI_CommonCallback() {
                        @Override
                        public void onSuccess() {

                        }
                    });
                    warnDialog.setCallBack(new FreeUI_CommonCallback() {
                        @Override
                        public void onSuccess() {
                            if (isVirating) {
                                isVirating = false;
                                VibratorUtils.virateCancle(BasePollingActivity.this);
                            }
                        }
                    });
                    if (!isVirating) {
                        isVirating = true;
                        VibratorUtils.vibrate(BasePollingActivity.this, new long[]{500, 1000}, 0);
                    }
                    warnDialog.show(getSupportFragmentManager(), "UnLock_DIALOG");
                }
            }
        }
    }
}
